package com.gitee.jisong;

import com.alibaba.fastjson.util.TypeUtils;
import org.apache.commons.lang3.math.NumberUtils;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author wb.sunjisong18 2020-06-09
 */
public class GSONMap extends HashMap<String,Object> {

    public GSONMap(int initialCapacity, float loadFactor) {
        super(initialCapacity, loadFactor);
    }

    public GSONMap(int initialCapacity) {
        super(initialCapacity);
    }

    public GSONMap() {
        super(16, 0.75f);
    }

    public GSONMap(Map<? extends String, ?> m) {
        super(m);
    }

    public String getString(String key, String defaultValue){
        Object o = get(key);
        if(null == o) return defaultValue;
        return o.toString();
    }

    public byte getByte(String key, byte defaultValue){
        return NumberUtils.toByte(getString(key, null),defaultValue);
    }

    public short getShort(String key, short defaultValue){
        return NumberUtils.toShort(getString(key, null),defaultValue);
    }

    public int getInt(String key , int defaultValue){
        return NumberUtils.toInt(getString(key, null),defaultValue);
    }
    public long getLong(String key , long defaultValue){
        return NumberUtils.toLong(getString(key, null),defaultValue);
    }

    public float getFloat(String key, float defaultValue){
        return NumberUtils.toFloat(getString(key, null),defaultValue);
    }

    public double getDouble(String key, double defaultValue){
        return NumberUtils.toDouble(getString(key, null),defaultValue);
    }
    //default zero
    public BigDecimal getBigDecimal(String key){
        return NumberUtils.toScaledBigDecimal(getString(key, null));
    }

    //default zero
    public BigDecimal getBigDecimal(String key, int scale , RoundingMode RountMode){
        return NumberUtils.toScaledBigDecimal(getString(key, null),scale,RountMode);
    }

    public Date getDate(String key){
        return getDate(key,GSON.DEFFAULT_DATE_FORMAT);
    }

    public Date getDate(String key , String format){
        return getDate(key,format,null);
    }

    public Date getDate(String key , String format , Date defaultValue){
        Object obj = get(key);
        if(null == obj) return defaultValue;
        if(obj instanceof Date){ // 使用频率最高的，应优先处理
            return (Date) obj;
        }

        if(obj instanceof Calendar){
            return ((Calendar) obj).getTime();
        }
        SimpleDateFormat sdf = new SimpleDateFormat(format,GSON.defaultLocale);
        sdf.setTimeZone(GSON.defaultTimeZone);
        try {
            return sdf.parse(obj.toString());
        } catch (ParseException e) {

        }
        return defaultValue;
    }

    public <T> T getBean(String key, Class<T> clazz){

        Object obj = get(key);
        if(null == obj) return null;
        if(clazz == obj.getClass()){
            return (T) obj;
        }

        if(clazz.isAssignableFrom(obj.getClass())){
            return (T) obj;
        }

        if(obj instanceof Map){
            if(clazz == Map.class){
                return (T) obj;
            }
            String s = GSON.toJSONString(obj);
            return GSON.parseObject(s,clazz);
        }
        if(obj instanceof String){
            return GSON.parseObject((String)obj,clazz);
        }
        return null;
    }

    public GSONMap getGSONMap(String key){
        return getGSONMap(key,new GSONMap());
    }

    public GSONMap getGSONMap(String key,GSONMap defaultValue){
        Object o = get(key);
        if(null == o) return defaultValue;
        if(o instanceof Map){
            return new GSONMap((Map)o);
        }
        if(o instanceof String){
            return GSON.parseMap((String) o);
        }
        return defaultValue;
    }

    public GSONArray getGSONArray(String key){
        return getGSONArray(key,new GSONArray());
    }

    public GSONArray getGSONArray(String key, GSONArray defaultValue){
        Object o = get(key);
        if(null == o) return defaultValue;
        if(o instanceof Collection){
            return new GSONArray((Collection) o);
        }
        if(o instanceof String){
            return GSON.parseArray((String) o);
        }
        return defaultValue;
    }
}
